package com.jiepos.mpos.biz.code.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Code;
import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.MenuButtonDao;
import com.jiepos.mpos.common.entity.App_MenuButton_InfEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;

@Service
public class MenuButtonService extends BaseService{
	
	@Autowired
	private MenuButtonDao menuButtonDao;
	
	/**
	 * 列表查询
	 * @param pagination
	 * @param muuid 菜单编码
	 * @return
	 */
	public Map<String, Object> getMenuBtnPageList(Pagination pagination, String muuid){
		StringBuffer sql = getInitSql("t1").append(",");
		sql.append("t1.button_uuid uuid, t1.button_code, t1.button_name, t1.button_orders, t1.button_url, t1.function_name, t1.button_iconcls,")
		.append("t1.button_type || '-' || t2.code_desc button_type,")
		.append("t1.is_show_list || '-' || t3.code_desc is_show_list")
		.append(" FROM ")
		.append(Table.T_APP_MENUBUTTON_INF).append(" t1")
		.append(" LEFT JOIN ").append(Table.T_APP_CODE_LIST).append(" t2 ON t1.button_type = t2.code_val and t2.code_tp = '").append(Code.BUTTON_TYPE).append("'")
		.append(" LEFT JOIN ").append(Table.T_APP_CODE_LIST).append(" t3 ON t1.is_show_list = t3.code_val and t3.code_tp = '").append(Code.IS_SHOW_LIST).append("'")
		.append(" where 1 = 1");
		List<String> list = new ArrayList<>();
		if(StringUtils.isNotEmpty(muuid)){
			sql.append(" and t1.menu_uuid = ?");
			list.add(muuid);
		}
		pagination.setQueryString(sql.toString());
		pagination = getOrderSql(pagination, "t1");
		return menuButtonDao.getPaginationMap(pagination, "t1", list);
	}
	
	/**
	 * 根据菜单按钮唯一标识查询实体
	 * @param button_uuid 按钮唯一 标识
	 * @return
	 */
	public App_MenuButton_InfEntity getMenuBtnByUuid(String button_uuid){
		if (StringUtils.isBlank(button_uuid)) {
			return new App_MenuButton_InfEntity();
		}
		return menuButtonDao.findByParam("button_uuid", button_uuid);
	}

	/**
	 * 保存菜单和按钮关系
	 * @param btnEntity
	 * @param map
	 */
	public void saveMenu(App_MenuButton_InfEntity btnEntity, Map map) {
		btnEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
		if (!StringUtils.isBlank(btnEntity.getButton_uuid())) {
			App_MenuButton_InfEntity oldEntity = menuButtonDao.findByParam("button_uuid", btnEntity.getButton_uuid());
			btnEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			btnEntity.setRec_id(oldEntity.getRec_id());
			btnEntity.setRec_crt_ts(oldEntity.getRec_crt_ts());
			btnEntity.setLast_oper_in(SystemConstant.OPER_UPDATE);
			List noChangeList = new ArrayList();
			noChangeList.add("button_uuid");
			noChangeList.add("menu_uuid");
			menuButtonDao.update(btnEntity,noChangeList);
		} else {
			btnEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			btnEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			btnEntity.setButton_uuid(IDFactoryUtil.getUUID());
			btnEntity.setRec_st(SystemConstant.NORMAL_STATUS);
			btnEntity.setLast_oper_in(SystemConstant.OPER_ADD);
			menuButtonDao.save(btnEntity);
		}
	}
	
	/**
	 * 删除菜单按钮
	 * @param button_uuids
	 */
	public void deleteMenuBtnByIds(String button_uuids){
		menuButtonDao.deleteByStatus("button_uuid", button_uuids);
	}
	
	/**
	 * 根据按钮代码集合查询按钮数据
	 * @param button_uuids
	 * @return
	 */
	public List<App_MenuButton_InfEntity> getUrlsByBtnUuids(String button_uuids){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM ").append(Table.T_APP_MENUBUTTON_INF)
		.append(" WHERE BUTTON_UUID IN (").append(button_uuids).append(")");
		List<App_MenuButton_InfEntity> list = menuButtonDao.queryForListByParam(getOrderSql(sql.toString(), null));
		return list;
	}
}
